<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #template {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="template">
      <!-- @fallback="fallback" -->
      <div class="container">
        <test />
      </div>
    </div>
    <div id="app"></div>
    <script src="../../../../packages/vue/dist/vue.global.js"></script>

    <script>
      const { createApp, ref, defineAsyncComponent } = Vue

      const TodoListPromise = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({
            template: `
                    <ul>
                        <li>1</li>
                        <li>2</li>
                        <li>3</li>
                    </ul>
                  `
          })
        }, 2000)
      })

      const template = document.querySelector('#template')?.innerHTML
      const App = {
        setup() {
          const fallback = () => {
            console.log('fallback')
          }

          const pending = () => {
            console.log('pending')
          }

          const resolve = () => {
            console.log('resolve')
          }

          const msg = ref('message----')
          return {
            msg,
            fallback,
            resolve,
            pending
          }
        },
        components: {
          Test: {
            template: `
            <suspense
              @pending="pending"
              @fallback="() => { console.log('msg:', msg) }"
            >
              <template v-slot:default>
                <todo-list />
              </template>
              <template v-slot:fallback>
                <h1>loading...</h1>
              </template>
            </suspense>
            `,
            components: {
              TodoList: defineAsyncComponent(() => TodoListPromise),
            }
          }
        },
        template
      }

      createApp(App).mount('#app')
    </script>
  </body>
</html>
